<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class FBController extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/main';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $FBRequest;
	public $FBoauth;
    public $FBUser;
    public $FBUser_profile;
    public $FBReturn;
    public $facebook;
	public $sidebar='SideBar';
	public $logged_in=false;
	public $sidebaritems=array(
						'title' 		=> null,
						'hasNews'		=> true,
						'hasInfoOnly' 	=> false,
						'infoText' 		=> null,
						);
	public $menu=array();
	public $robots='noindex,nofollow';
	public $description;
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
	
	private $_session;
	
	public function init(){
		$this->_session=new CHttpSession;
		$this->_session->open();
		if(isset(Yii::app()->params['layout'])) $this->layout = Yii::app()->params['layout'];
		/*
		$page=new Page;
		$this->menu=$page->getMenu(Yii::app()->params['siteId']);
		$info=$page->getRobots($_SERVER['REQUEST_URI']);
		$this->robots=$info['robots'];
		$this->description=$info['description'];
		*/
		/* Facebook inits */
		$this->facebook = new Facebook(array(
  			'appId'  => '372024222847571',
  			'secret' => 'ecf8b18ac592026bb882cb763108bd57',
  			'cookie' => true,
		));
		$this->FBoauth = $this->facebook->getAccessToken();
		//$theRequest = $this->facebook->getSignedRequest();
		//$this->facebook->setAccessToken($theRequest['oauth_token']);
     	$user = $this->facebook->getUser(); 
        //$this->FBRequest = $theRequest;
        $this->FBUser = $user;
		// if we have an id
		if($user) {
			// from the offset, we're good to go...
			$this->logged_in = true;
			//$this->facebook->setAccessToken($theRequest['oauth_token']);
			$this->FBUser_profile = $this->facebook->api('/me');
			$params = array( 'next' => 'http://fb.audelion.ch?logged_out' );
			$return .= '<br /><a href="' . $this->facebook->getLogoutUrl($params) . '">logout</a>';
		
		}else{
			$login_url = $this->facebook->getLoginUrl( 
				array( 
					'scope' => 'user_photos, user_photo_video_tags, offline_access, read_stream, publish_stream, publish_actions, share_item, photo_upload, user_notes ',
					'next' => 'http://fb.audelion.ch/login.html' 
				) 
			);
			$return .= 'Please <a href="' . $login_url . '">login.</a>';
		
		}
	$this->FBReturn=$return;	
	}
}